/*                                  ATELIER                                   */

package edu.gmu.view.canvas.graph;

import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.ImageIcon;

/**
 * This class provides caching for commonly used symbols.  It is used
 * as part of Fly Weight Design Pattern where an image only exists
 * once in memory but is "stamped" many times onto a canvas (i.e. component).
 *
 * @author  James H. Pope
 * @version $Revision:$ $Date:$
 */
public final class ImageCache
{
    /**
     * Singleton instance of this object.
     */
    public static ImageCache INSTANCE = null;

    /**
     * Map of symbols
     */
    public static Map <String,ImageIcon> SYMBOLS = new LinkedHashMap <String,ImageIcon> ();

    /**
     * Gets the ImageIcon for the given fileName.  If it has not
     * Already been loaded, the image is loaded and placed into the
     * map - susequent requests simply return the loaded map value.
     * @param fileName
     * @return
     */
    public static ImageIcon getImageIcon( String fileName )
    {
        // Does it already exist in the map?
        String key = fileName;
        ImageIcon imageIcon = SYMBOLS.get(key);
        if( imageIcon == null )
        {
            // Load it and store in map
            //imageIcon = makeImageIcon( fileName );
            imageIcon = ImageFactory.makeImageIcon( fileName );

            SYMBOLS.put(key, imageIcon);
        }
        return imageIcon;
    }

    /**
     * Gets the ImageIcon for the given fileName.  If it has not
     * Already been loaded, the image is loaded and placed into the
     * map - susequent requests simply return the loaded map value.
     * @param fileName
     * @param width
     * @param height
     * @return
     */
    public static ImageIcon getImageIcon( String fileName, int width, int height )
    {
        // Does it already exist in the map?
        String key = fileName + width + height;
        ImageIcon imageIcon = SYMBOLS.get(key);
        if( imageIcon == null )
        {
            // Load it and store in map
            //imageIcon = makeImageIcon( fileName );
            imageIcon = ImageFactory.makeImageIcon( fileName, width, height );

            SYMBOLS.put(key, imageIcon);
        }
        return imageIcon;
    }


    
}

/*                                  ATELIER                                   */